/*
 * @Author: your name
 * @Date: 2021-04-17 10:03:47
 * @LastEditTime: 2021-04-17 11:14:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /flutter_demo/lib/pages/NewsContent.dart
 */
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class NewsContentPage extends StatefulWidget {
  Map arguments;
  NewsContentPage({Key key, this.arguments}) : super(key: key);

  @override
  _NewsContentPageState createState() => _NewsContentPageState(this.arguments);
}

class _NewsContentPageState extends State<NewsContentPage> {
  Map arguments;
  List _list = [];
  bool _flag = true;
  _NewsContentPageState(this.arguments);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getContent();
  }

  _getContent() async {
    var url =
        'http://www.phonegap100.com/appapi.php?a=getPortalArticle&aid=${this.arguments["aid"]}';
    var result = await Dio().get(url);
    // _list = json.decode(result.data)['result'];
    setState(() {
      _list = json.decode(result.data)['result'];
    });
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text('加载中...', style: TextStyle(fontSize: 16)),
            CircularProgressIndicator(
              strokeWidth: 1,
            )
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('新闻详情'),
        ),
        body: Column(
          children: <Widget>[
            this._flag ? _getMoreWidget() : Text(''),
            Expanded(
              child: InAppWebView(
                initialUrlRequest: URLRequest(
                    url: Uri.parse(
                        "https://www.phonegap100.com/newscontent.php?aid=${this._list[0]['aid']}")),
                initialOptions: InAppWebViewGroupOptions(
                    crossPlatform: InAppWebViewOptions(
                      useShouldOverrideUrlLoading: true,
                      mediaPlaybackRequiresUserGesture: false,
                    ),
                    android: AndroidInAppWebViewOptions(
                      useHybridComposition: true,
                    ),
                    ios: IOSInAppWebViewOptions(
                      allowsInlineMediaPlayback: true,
                    )),
                onProgressChanged:
                    (InAppWebViewController controller, int progress) {
                  if (progress / 100 > 0.999) {
                    setState(() {
                      this._flag = false;
                    });
                  }
                },
              ),
            ),
            // Text('${this._list.length > 0 ? this._list[0]["title"] : ""}'),
            // Text('${this._list.length > 0 ? this._list[0]["content"] : ""}'),
            // Html(data: this._list[0]["content"])
          ],
        ));
  }
}
